package com.chalk.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.common.constant.CommonConstants;
import com.chalk.common.util.Result;
import com.chalk.common.util.ResultCode;
import com.chalk.config.base.BaseController;
import com.chalk.model.SysParam;
import com.chalk.ro.SysParamRo;
import com.chalk.service.SysParamService;
import com.chalk.service.SysUserService;
import com.chalk.vo.SysParamVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 系统参数表(SysParam)表控制层
 *
 * @author maxiaoqiang
 * @since 2019-03-22 17:38:25
 */
@Api(value = "系统参数表接口", tags = "系统参数表模块")
@RestController
@RequestMapping("/v1/sysParam")
public class SysParamController extends BaseController {

    @Autowired
    private SysParamService sysParamService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 系统参数表列表
     *
     * @param current  当前页数
     * @param size     每页显示条数
     * @param sysParam 查询实体
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询系统参数表列表", notes = "查询系统参数表列表", response = Result.class)
    @GetMapping
    public Result selectAll(Integer current, Integer size, SysParamVo sysParam) {
        sysParam.setIsDeleted(CommonConstants.IsDeleted.NOTDEL.getValue());
        Page<SysParamVo> List = this.sysParamService.selectPageVo(this.getPageVo(current, size), sysParam);
        return Result.success(List);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询系统参数表", notes = "查询系统参数表", response = Result.class)
    @GetMapping("{id}")
    public Result selectOne(
            @ApiParam(name = "id", value = "主键", required = true, example = "1")
            @PathVariable String id) {
        SysParam param = new SysParam();
        param.setParamKey(id);
        param.setIsDeleted(CommonConstants.IsDeleted.NOTDEL.getValue());
        return Result.success(this.sysParamService.selectDtoById(param));
    }


    /**
     * 保存系统参数表
     *
     * @param sysParam 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "保存sysParam", notes = "保存sysParam", response = Result.class)
    @PostMapping
    public Result insert(
            @ApiParam(name = "sysParam", value = "传入json格式", required = true)
            @RequestBody @Valid SysParamRo sysParam, BindingResult bindingResult) {
        beanValidator(bindingResult);
        // paramKey 校验
        SysParam param = new SysParam();
        param.setParamKey(sysParam.getParamKey());
        param.setIsDeleted(CommonConstants.IsDeleted.NOTDEL.getValue());
        int i = this.sysParamService.selectCount(new EntityWrapper<>(param));
        if (i > 0) {
            return Result.fail(ResultCode.PARAM_KEY_REPEAT);
        }
        DateTime date = DateUtil.date();
        sysParam.setCreateTime(date);
        sysParam.setUpdateTime(date);
        //throw new ServiceException(ResultCode.SYS_ERROR);
        return Result.success(this.sysParamService.insert(sysParam));
    }

    /**
     * 修改系统参数表
     *
     * @param sysParam 实体对象
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "修改系统参数表", notes = "修改系统参数表", response = Result.class)
    @PutMapping
    public Result update(
            @ApiParam(name = "sysParam", value = "传入json格式", required = true)
            @RequestBody @Valid SysParamRo sysParam, BindingResult bindingResult) {
        beanValidator(bindingResult);
        sysParam.setUpdateTime(DateUtil.date());
        return Result.success(this.sysParamService.updateById(sysParam));
    }

    /**
     * 逻辑删除系统参数表
     *
     * @param ids 主键集合
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "批量删除系统参数表", notes = "批量删除系统参数表", response = Result.class)
    @DeleteMapping
    public Result delete(
            @ApiParam(name = "ids", value = "传数组", required = true)
            @RequestBody String[] ids) {
        return Result.success(this.sysParamService.deleteBatchByPrimaryKey(ids));
    }

    private Page<SysParam> getPage(Integer current, Integer size) {
        Page<SysParam> page = new Page<>();
        if (current != null) {
            page.setCurrent(current);
        }
        if (size != null) {
            page.setSize(size);
        }
        return page;
    }

    private Page<SysParamVo> getPageVo(Integer current, Integer size) {
        Page<SysParamVo> page = new Page<>();
        if (current != null) {
            page.setCurrent(current);
        }
        if (size != null) {
            page.setSize(size);
        }
        return page;
    }


    /**
     * 信息管理
     *
     * @param current  当前页数
     * @param size     每页显示条数
     * @param sysParam 查询实体
     * @return Result 返回统一结果model
     */
    @ApiOperation(value = "查询系统参数表列表", notes = "查询系统参数表列表", response = Result.class)
    @GetMapping("/getInfo")
    public Result getInfo(Integer current, Integer size, SysParam sysParam) {
        return Result.success(this.sysParamService.selectPage(this.getPage(current, size), new EntityWrapper<SysParam>(sysParam)
                .eq("is_deleted",CommonConstants.IsDeleted.NOTDEL)
                .eq("service_type",1)));
    }

    /**
     * 修改信息管理
     * @param sysParam
     * @return
     */
    @ApiOperation(value = "修改信息管理", notes = "修改信息管理", response = Result.class)
    @PutMapping("/updateInfo")
    public Result updateInfo(
            @ApiParam(name = "sysParam", value = "传入json格式", required = true)
            @RequestBody SysParam sysParam) {
        return Result.success(this.sysParamService.updateById(sysParam));
    }
}